home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / tarmail / btoa.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  2KB  |  120 lines

  1. /* btoa: version 4.0
  2.  * stream filter to change 8 bit bytes into printable ascii
  3.  * computes the number of bytes, and three kinds of simple checksums
  4.  * incoming bytes are collected into 32-bit words, then printed in base 85
  5.  *  exp(85,5) > exp(2,32)
  6.  * the ASCII characters used are between '!' and 'u'
  7.  * 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
  8.  *
  9.  *  Paul Rutter        Joe Orost
  10.  *  philabs!per        petsd!joe
  11.  *
  12.  *  WARNING: this version is not compatible with the original as sent out
  13.  *  on the net.  The original encoded from ' ' to 't'; which cause problems
  14.  *  with some mailers (stripping off trailing blanks).
  15.  *
  16.  *  900308 rpw3        Modified for slightly more user-friendly usage message
  17.  */
  18.  
  19. #include <stdio.h>
  20.  
  21. #define reg register
  22.  
  23. #define MAXPERLINE 78
  24.  
  25. long int Ceor = 0;
  26. long int Csum = 0;
  27. long int Crot = 0;
  28.  
  29. long int ccount = 0;
  30. long int bcount = 0;
  31. long int word;
  32.  
  33. #define EN(c)    (int) ((c) + '!')
  34.  
  35. encode(c) 
  36.   reg c;
  37. {
  38.   Ceor ^= c;
  39.   Csum += c;
  40.   Csum += 1;
  41.   if ((Crot & 0x80000000)) {
  42.     Crot <<= 1;
  43.     Crot += 1;
  44.   } else {
  45.     Crot <<= 1;
  46.   }
  47.   Crot += c;
  48.  
  49.   word <<= 8;
  50.   word |= c;
  51.   if (bcount == 3) {
  52.     wordout(word);
  53.     bcount = 0;
  54.   } else {
  55.     bcount += 1;
  56.   }
  57. }
  58.  
  59. wordout(word) 
  60.   reg long int word;
  61. {
  62.   if (word == 0) {
  63.     charout('z');
  64.   } else {
  65.     reg int tmp = 0;
  66.     
  67.     if(word < 0) {    /* Because some don't support unsigned long */
  68.       tmp = 32;
  69.       word = word - (long)(85 * 85 * 85 * 85 * 32);
  70.     }
  71.     if(word < 0) {
  72.       tmp = 64;
  73.       word = word - (long)(85 * 85 * 85 * 85 * 32);
  74.     }
  75.     charout(EN((word / (long)(85 * 85 * 85 * 85)) + tmp));
  76.     word %= (long)(85 * 85 * 85 * 85);
  77.     charout(EN(word / (85 * 85 * 85)));
  78.     word %= (85 * 85 * 85);
  79.     charout(EN(word / (85 * 85)));
  80.     word %= (85 * 85);
  81.     charout(EN(word / 85));
  82.     word %= 85;
  83.     charout(EN(word));
  84.   }
  85. }
  86.  
  87. charout(c) {
  88.   putchar(c);
  89.   ccount += 1;
  90.   if (ccount == MAXPERLINE) {
  91.     putchar('\n');
  92.     ccount = 0;
  93.   }
  94. }
  95.  
  96. main(argc,argv) 
  97.   char **argv;
  98. {
  99.   reg c;
  100.   reg long int n;
  101.  
  102.   if (argc != 1) {
  103.     fprintf(stderr,"%s: no args allowed, please use pipes or re-direction\n", argv[0]);
  104.     exit(2);
  105.   }
  106.   printf("xbtoa Begin\n");
  107.   n = 0;
  108.   while ((c = getchar()) != EOF) {
  109.     encode(c);
  110.     n += 1;
  111.   }
  112.   while (bcount != 0) {
  113.     encode(0);
  114.   }
  115.   /* n is written twice as crude cross check*/
  116.   printf("\nxbtoa End N %ld %lx E %lx S %lx R %lx\n", n, n, Ceor, Csum, Crot);
  117.   exit(0);
  118. }
  119.  
  120.